package com.explorati.tree.LeetCode257.BinaryTreePath;

import java.util.ArrayList;
import java.util.List;

/**
 * 257. Binary Tree Paths
 * 
 * @author explorati
 *
 */
public class Solution {
	public class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;

		TreeNode(int x) {
			val = x;
		}
	}

	public List<String> binaryTreePaths(TreeNode root) {
		List<String> res = new ArrayList<String>();
		if (root == null) {
			return res;
		}
		if (root.left == null && root.right == null) {
			res.add(Integer.toString(root.val));
			return res;
		}

		List<String> leftPaths = binaryTreePaths(root.left);
		for (String s : leftPaths) {
			StringBuffer sb = new StringBuffer(Integer.toString(root.val));
			sb.append(s);
			sb.append("->");
			res.add(sb.toString());
		}

		List<String> rightPaths = binaryTreePaths(root.left);
		for (String s : rightPaths) {
			StringBuffer sb = new StringBuffer(Integer.toString(root.val));
			sb.append(s);
			sb.append("->");
			res.add(sb.toString());
		}
		return res;
	}
}
